package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.repository.StrategyCommentMongoRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentMongoRepository repository;
    @Autowired
    private MongoTemplate template;


    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        repository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        repository.save(strategyComment);
    }

    @Override
    public StrategyComment find(String id) {
        StrategyComment strategyComment = repository.findById(id).orElse(null);

        return strategyComment;
    }

    @Override
    public List<StrategyComment> findAll() {
        List<StrategyComment> list = repository.findAll();
        return list;
    }

    @Override
    public void addComment(StrategyComment strategyComment, UserInfo userInfo) {
        //将userInfo赋值到strategyComment的属性中,通过内省的方式,属性名必须相同
        BeanUtils.copyProperties(userInfo,strategyComment);
        //属性名不同的id单独设置给它
        strategyComment.setUserId(userInfo.getId());
        strategyComment.setCreateTime(new Date());
        this.save(strategyComment);
    }

    @Override
    public Page query(StrategyCommentQueryObject qo) {
        Query query = new Query();
        if(StringUtils.hasLength(qo.getStrategyId())){
           query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        long count = template.count(query, StrategyComment.class);
        if(count == 0){
            return Page.empty();
        }
        Pageable page = PageRequest.of(qo.getCurrentPage() - 1,qo.getPageSize(),Sort.Direction.ASC,"_id");
        query.with(page);
        List<StrategyComment> list = template.find(query, StrategyComment.class);
        return new PageImpl(list,page,count);
    }

    @Override
    public void commentThumb(String cid, UserInfo userInfo) {
        //查询评论对象
        StrategyComment comment = this.find(cid);
        List<String> thumbuplist = comment.getThumbuplist();
        if(thumbuplist.contains(userInfo.getId())){
            //包含当前用户对象id,为取消点赞操作,删除集合中元素,点赞数-1
            thumbuplist.remove(userInfo.getId());
            comment.setThumbupnum(comment.getThumbupnum() - 1);
        }else{
            //不包含当前用户对象id,为点赞操作,增加集合中元素,点赞数+1
            thumbuplist.add(userInfo.getId());
            comment.setThumbupnum(comment.getThumbupnum() + 1);
        }
        //更新到数据库中
        this.update(comment);
    }


}
